﻿#region Header

/*
Remote views based on linq
By

Pascal Hauser 
Dipl. Ing. in Informatik, Hochschule für Technik Rapperswil, 2006
Master Thesis, Hochschule für Technik Rapperswil, 2008-2010

and

Raphael Gfeller
Dipl. Ing. in Informatik, Hochschule für Technik Rapperswil, 2006
Master Thesis, Hochschule für Technik Rapperswil, 2008-2010

*/

#endregion

#region Usings

using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Sebarf.Utils.Interfaces;

#endregion

namespace Utils.Test {
	/// <summary>
	/// Tests the <see cref="HieararchyFlattener"/>
	/// </summary>
	[TestClass]
	public class TestHieararchyFlattener {
		#region Test Methods

		[TestMethod]
		public void TestHieararchyFlattenenObject1() {
			var value1 = new Object();
			var objectToTest = new { Name = "Hallo", Data = value1 };
			Assert.IsTrue(new ObjectHierarchyFlattenerService().Flatten(objectToTest).Contains(value1));
		}

		[TestMethod]
		public void TestHieararchyFlattenenDictionary1() {
			var value1 = new Object();
			var objectToTest = new { Name = "Hallo", Data = new Dictionary<String, Object>() };
			objectToTest.Data.Add("Item1", value1);

			Assert.IsTrue(new ObjectHierarchyFlattenerService().Flatten(objectToTest).Contains(value1));
		}

		[TestMethod]
		public void TestHieararchyFlattenenDictionary2() {
			var value1 = new Object();
			var objectToTest = new { Name = "Hallo", Items = new { Data = new Dictionary<String, Object>() } };
			objectToTest.Items.Data.Add("Item1", value1);

			Assert.IsTrue(new ObjectHierarchyFlattenerService().Flatten(objectToTest).Contains(value1));
		}

		[TestMethod]
		public void TestHieararchyFlattenenValueType1() {
			var value1 = new Object();
			var objectToTest = new { Name = "Hallo", Items = new { Data = new Dictionary<String, Object>() } };
			objectToTest.Items.Data.Add("Item1", value1);

			Assert.IsFalse(new ObjectHierarchyFlattenerService().Flatten(objectToTest).Contains("Hallo"));
		}

		[TestMethod]
		public void TestHieararchyFlattenenValueType2() {
			var value1 = new Object();
			var objectToTest = new { Id = 2, Name = "Hallo", Items = new { Data = new Dictionary<String, Object>() } };
			objectToTest.Items.Data.Add("Item1", value1);

			Assert.IsFalse(new ObjectHierarchyFlattenerService().Flatten(objectToTest).Contains(2));
		}

		[TestMethod]
		public void TestHieararchyFlattenenRecursion() {
			var value1 = new Object();
			var objectToTest = new { Id = 2, Name = "Hallo", Items = new { Data = new Dictionary<String, Object>() } };
			var secondObject = new { Item = objectToTest };
			objectToTest.Items.Data.Add("Item1", value1);
			objectToTest.Items.Data.Add("Item2", secondObject);

			Assert.IsTrue(new ObjectHierarchyFlattenerService().Flatten(objectToTest).Contains(secondObject));
			Assert.IsTrue(new ObjectHierarchyFlattenerService().Flatten(secondObject).Contains(objectToTest));
		}

		[TestMethod]
		public void TestHieararchyFlattenenEvents() {
			var value1 = new Object();
			var obj1 = new DummyClass1();
			obj1.Handler += delegate { };
			var objectToTest = new { Id = 2, Name = "Hallo", Items = new { Data = new Dictionary<String, Object>() } };
			objectToTest.Items.Data.Add("Item1", obj1);
			objectToTest.Items.Data.Add("Item2", new DummyClass1());
			objectToTest.Items.Data.Add("Item3", objectToTest);
			Assert.IsTrue(new ObjectHierarchyFlattenerService().Flatten(objectToTest).Contains(obj1));
		}

		#endregion

		#region Nested type: DummyClass1

		private class DummyClass1 {
			public event EventHandler Handler;
		}

		#endregion
	}
}